home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 005 / mandelbrotsrc / mand5.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  12KB  |  442 lines

  1.  
  2. /*****************************************************************************
  3.  *                     MAND5.C - Color Window
  4.  *             Mandelbrot Self-Squared Dragon Generator
  5.  *                    For the Commodore Amiga
  6.  *                         Version 2.01
  7.  *
  8.  *             Copyright (C) 1986, =Robert J. Mical=
  9.  *                  Placed in the Public Domain
  10.  *
  11.  *  This program may be distributed free of charge as long as the above
  12.  *  notice is retained.  You may extract any or all information contained
  13.  *  in this file for use in your own programs
  14.  *
  15.  ****************************************************************************/
  16.  
  17. #include "mand.h"
  18.  
  19.  
  20. extern struct Menu MainMenu[MENU_COUNT];
  21. extern struct MenuItem OptionsItems[OPTIONS_COUNT];
  22.  
  23. extern int MathBase, MathTransBase;
  24.  
  25. extern SHORT Color0, Color1, Color2;
  26. extern SHORT UserPalette[29];
  27.  
  28. /*----------------------*/
  29. /* Graphics definitions */
  30.  
  31. extern struct   GfxBase       *GfxBase;
  32. extern struct   IntuitionBase *IntuitionBase;
  33.  
  34. extern struct   RastPort      *rp,*rp2;
  35. extern struct   ViewPort      *vp;
  36.  
  37. extern struct   Window        *w,*w2, *ColorWindow;
  38. extern struct   Screen        *screen;
  39. extern struct   IntuiMessage  *message;
  40.  
  41. extern long last_color;
  42.  
  43. extern BOOL SettingCenter, SettingBoxSize;
  44.  
  45. /*----------------------------------*/
  46. /* Miscellaneous Global Definitions */
  47.  
  48. extern union kludge {
  49.    float f;
  50.    int i;
  51. } start_r,end_r,start_i,end_i;  /* Block bounds for set */
  52. extern int max_x,max_y,max_mem_y;  /* Graphics window size */
  53. extern int max_count,color_inc,color_offset,color_set,color_mode,color_div;
  54. extern int color_inset,func_num;
  55.  
  56. extern int v_starty,max_mem;
  57. extern long v_offset;
  58. extern UWORD *color_table,*v_mand_store;
  59.  
  60. extern int modified,want_read;
  61.  
  62. extern FILE *console,*v_fp,*redir_fp;
  63.  
  64. extern SHORT ZoomCenterX, ZoomCenterY, ZoomBoxSizeX, ZoomBoxSizeY;
  65. extern SHORT ZoomBoxStartX, ZoomBoxStartY;
  66.  
  67.  
  68.  
  69. /*****************************************************************************
  70.  *
  71.  * Color initialization and routines
  72.  *
  73.  ****************************************************************************/
  74.  
  75. #define COPYCOLOR    1
  76. #define RANGE_FIRST  2
  77. #define RANGE_SECOND 3
  78. USHORT ColorMode;
  79.  
  80. extern struct Gadget ColorTemplateGadgets[COLOR_GADGETS_COUNT];
  81. extern struct Image ColorPropsImages[3];
  82. extern struct PropInfo ColorPropsInfos[3];
  83. extern struct IntuiText ColorClusterText[4];
  84.  
  85. USHORT SavePalette[32];
  86. extern struct Image SuperColorImages[32];
  87.  
  88. #define COLOR_IDCMP_FLAGS (GADGETDOWN | GADGETUP | MOUSEBUTTONS \
  89.         | MENUPICK | MOUSEMOVE | ACTIVEWINDOW | INACTIVEWINDOW)
  90.  
  91.  
  92. struct NewWindow ColorNewWindow =
  93.     {
  94.     /*  SHORT LeftEdge, TopEdge;        /* screen dimensions of window */
  95.     /*  SHORT Width, Height;            /* screen dimensions of window */
  96.     DEFAULT_WINDOW_LEFT, DEFAULT_WINDOW_TOP,
  97.     COLORWINDOW_WIDTH, COLORWINDOW_HEIGHT,
  98.  
  99.     /*  UBYTE DetailPen, BlockPen;      /* for bar/border/gadget rendering */
  100.     -1, -1,
  101.  
  102.     /*  ULONG IDCMPFlags;               /* User-selected IDCMP flags */
  103.     NULL,
  104.  
  105.     /*  ULONG Flags;                    /* see Window struct for defines */
  106.     BORDERLESS | SMART_REFRESH | NOCAREREFRESH,
  107.  
  108.     /*  struct Gadget *FirstGadget;*/
  109.     NULL,
  110.  
  111.     /*  struct Image *CheckMark;*/
  112.     NULL,
  113.  
  114.     /*  UBYTE *Title;                     /* the title text for this window */
  115.     NULL,
  116.     
  117.     /*  struct Screen *Screen;*/
  118.     NULL,
  119.     
  120.     /*  struct BitMap *BitMap;*/
  121.     NULL,
  122.  
  123.     /*  SHORT MinWidth, MinHeight;       /* minimums */
  124.     0, 0,
  125.     /*  SHORT MaxWidth, MaxHeight;       /* maximums */
  126.     0, 0,
  127.  
  128.     /*  USHORT Type;*/
  129.     CUSTOMSCREEN,
  130. };
  131.  
  132. USHORT RangeFirst;      /* the first selection of the range-color pair */
  133.  
  134.  
  135. /* ======================================================================== */
  136. /* ======================================================================== */
  137. /* ======================================================================== */
  138.  
  139. BOOL OpenColorWindow()
  140. {
  141.     SHORT i;
  142.  
  143.     if (ColorWindow) return(TRUE);
  144.  
  145.     ClearMenuStrip(w);
  146.     ColorNewWindow.Screen = screen;
  147.     ColorNewWindow.IDCMPFlags = NULL;
  148.     ColorNewWindow.FirstGadget
  149.             = &ColorTemplateGadgets[COLOR_GADGETS_COUNT - 1];
  150.     SetColorProps();
  151.     InitSuperColors();
  152.     ColorMode = NULL;
  153.  
  154.     if ((ColorWindow = (struct Window *)OpenWindow(&ColorNewWindow)) == 0)
  155.         {
  156.         fputs("no memory!\n", console);
  157.         return(FALSE);
  158.         }
  159.     ColorWindow->UserPort = w->UserPort;
  160.     ModifyIDCMP(ColorWindow, COLOR_IDCMP_FLAGS);
  161.     SetMenuStrip(w, &MainMenu[0]);
  162.  
  163.     for (i = 0; i < 32; i++) SavePalette[i] = GetRGB4(vp->ColorMap, i);
  164.     DrawColorWindow();
  165.     return(TRUE);
  166. }
  167.  
  168.  
  169. VOID CloseColorWindow(accept)
  170. BOOL accept;
  171. {
  172.     SHORT i;
  173.  
  174.     if (ColorWindow == NULL) return;
  175.  
  176.     ClearMenuStrip(ColorWindow);
  177.     ColorWindow->UserPort = NULL;
  178.     CloseWindow(ColorWindow);
  179.     ColorWindow = NULL;
  180.  
  181.     if (NOT accept) LoadRGB4(vp, &SavePalette[0], 32);
  182.     else
  183.         {
  184.         color_set = 2;
  185.         Color0 = GetRGB4(vp->ColorMap, 0);
  186.         Color1 = GetRGB4(vp->ColorMap, 1);
  187.         Color2 = GetRGB4(vp->ColorMap, 2);
  188.         for (i = 2; i <= 31; i++)
  189.             {
  190.             if (i == 2) *(color_table) = GetRGB4(vp->ColorMap, 2);
  191.             else *(color_table + color_offset + (i - 2) * color_inc) 
  192.                     = GetRGB4(vp->ColorMap, i);
  193.             }
  194.         }
  195. }
  196.  
  197.  
  198. VOID ColorRange(first, last)
  199. SHORT first, last;
  200. {
  201.     SHORT i;
  202.     LONG whole, redfraction, greenfraction, bluefraction;
  203.     USHORT rgb;
  204.     SHORT firstred, firstgreen, firstblue;
  205.     SHORT lastred, lastgreen, lastblue;
  206.     SHORT workred, workgreen, workblue;
  207.  
  208.     if (first > last)
  209.         {
  210.         i = first;
  211.         first = last;
  212.         last = i;
  213.         }
  214.  
  215.     /* I need to see a spread of at least two, where there's at least one
  216.      * spot between the endpoints, else there's no work to do so I
  217.      * might as well just return now.
  218.      */
  219.     if (first >= last - 1) return;
  220.  
  221.     rgb = GetRGB4(vp->ColorMap, first);
  222.     firstred = (rgb >> 8) & 0xF;
  223.     firstgreen = (rgb >> 4) & 0xF;
  224.     firstblue = (rgb >> 0) & 0xF;
  225.  
  226.     rgb = GetRGB4(vp->ColorMap, last);
  227.     lastred = (rgb >> 8) & 0xF;
  228.     lastgreen = (rgb >> 4) & 0xF;
  229.     lastblue = (rgb >> 0) & 0xF;
  230.  
  231.     whole = (lastred - firstred) << 16;
  232.     redfraction = whole / (last - first);
  233.     whole = (lastgreen - firstgreen) << 16;
  234.     greenfraction = whole / (last - first);
  235.     whole = (lastblue - firstblue) << 16;
  236.     bluefraction = whole / (last - first);
  237.  
  238.     for (i = first + 1; i < last; i++)
  239.         {
  240.         lastred = (redfraction * (i - first) + 0x8000) >> 16;
  241.         workred = firstred + lastred;
  242.         lastgreen = (greenfraction * (i - first) + 0x8000) >> 16;
  243.         workgreen = firstgreen + lastgreen;
  244.         lastblue = (bluefraction * (i - first) + 0x8000) >> 16;
  245.         workblue = firstblue + lastblue;
  246.         SetRGB4(vp, i, workred, workgreen, workblue);
  247.         }
  248. }
  249.  
  250.  
  251. BOOL ColorGadgetGotten(gadget)
  252. struct Gadget *gadget;
  253. {
  254.     USHORT rgb, pen;
  255.     struct RastPort *RPort;
  256.     SHORT greenpos, redpos, bluepos;
  257.  
  258.     RPort = ColorWindow->RPort;
  259.  
  260.     /* Have we got a color specifier? */
  261.     if (gadget->GadgetID < 32)
  262.         {
  263.         /* Yes, it's a color gadget.  Set this pen number */
  264.         pen = gadget->GadgetID;
  265.         /* first, were we in COPY COLOR mode? */
  266.         if (ColorMode == COPYCOLOR)
  267.             {
  268.             /* ok, copy old color here first! */
  269.             rgb = GetRGB4(vp->ColorMap, rp->FgPen);
  270.             SetRGB4(vp, pen, rgb >> 8, rgb >> 4, rgb);
  271.             ColorMode = NULL;
  272.             }
  273.         if (ColorMode == RANGE_FIRST)
  274.             {
  275.             ColorMode = RANGE_SECOND;
  276.             RangeFirst = pen;
  277.             }
  278.         else if (ColorMode == RANGE_SECOND)
  279.             {
  280.             ColorMode = NULL;
  281.             ColorRange(RangeFirst, pen);
  282.             }
  283.         SetAPen(rp, pen);
  284.         rgb = GetRGB4(vp->ColorMap, pen);
  285.         ColorRectFill(RPort, pen);
  286.  
  287.         redpos = RemoveGadget(ColorWindow,
  288.                 &ColorTemplateGadgets[COLOR_RED]);
  289.         greenpos = RemoveGadget(ColorWindow,
  290.                 &ColorTemplateGadgets[COLOR_GREEN]);
  291.         bluepos = RemoveGadget(ColorWindow,
  292.                 &ColorTemplateGadgets[COLOR_BLUE]);
  293.         SetColorProps();
  294.         AddGadget(ColorWindow, &ColorTemplateGadgets[COLOR_BLUE], bluepos);
  295.         AddGadget(ColorWindow, &ColorTemplateGadgets[COLOR_GREEN], greenpos);
  296.         AddGadget(ColorWindow, &ColorTemplateGadgets[COLOR_RED], redpos);
  297.         RefreshGadgets(&ColorTemplateGadgets[COLOR_GADGETS_COUNT - 1], 
  298.                 ColorWindow);
  299.         }
  300.  
  301.     else switch (gadget->GadgetID)
  302.         {
  303.         case COLOR_OK:
  304.             CloseColorWindow(TRUE);
  305.             return(FALSE);
  306.             break;
  307.         case COLOR_CANCEL:
  308.             CloseColorWindow(FALSE);
  309.             return(FALSE);
  310.             break;
  311.         case COLOR_COPY:
  312.             ColorMode = COPYCOLOR;
  313.             break;
  314.         case COLOR_RANGE:
  315.             ColorMode = RANGE_FIRST;
  316.             break;
  317.         }
  318.     return(TRUE);
  319. }
  320.  
  321.  
  322. ModifyColors()
  323. {
  324.     USHORT pen, newred, newgreen, newblue;
  325.  
  326.     pen = w->RPort->FgPen;
  327.  
  328.     newred = ((struct PropInfo *)
  329.             ColorTemplateGadgets[COLOR_RED].SpecialInfo)->HorizPot >> 12;
  330.     newgreen = ((struct PropInfo *)
  331.             ColorTemplateGadgets[COLOR_GREEN].SpecialInfo)->HorizPot >> 12;
  332.     newblue = ((struct PropInfo *)
  333.             ColorTemplateGadgets[COLOR_BLUE].SpecialInfo)->HorizPot >> 12;
  334.  
  335.     SetRGB4(vp, pen, newred, newgreen, newblue);
  336. }
  337.  
  338.  
  339.  
  340. DrawColorWindow()
  341. {
  342.     struct RastPort *RPort;
  343.  
  344.     RPort = ColorWindow->RPort;
  345.  
  346.     ColorRectFill(RPort, rp->FgPen);
  347.     SetAPen(RPort, 1);
  348.     DrawBox(RPort, 1, 1, COLORWINDOW_WIDTH - 2, COLORWINDOW_HEIGHT - 2);
  349.     DrawBox(RPort, COLOR_BOX_LEFT - 2, COLOR_BOX_TOP - 2, 
  350.         COLOR_BOX_RIGHT + 2, COLOR_BOX_BOTTOM + 2);
  351.     DrawBox(RPort, COLOR_BOX_LEFT - 2, COLOR_COLOR_TOP - 2, 
  352.         COLOR_BOX_LEFT + (8 * 15) + 1, COLOR_COLOR_TOP + (4 * 10) + 1);
  353. }
  354.  
  355.  
  356.  
  357. SetColorProps()
  358. {
  359.     USHORT rgb, red, green, blue;
  360.  
  361.     rgb = GetRGB4(vp->ColorMap, rp->FgPen);
  362.     red = (rgb >> 8) & 0xF;
  363.     green = (rgb >> 4) & 0xF;
  364.     blue = (rgb >> 0) & 0xF;
  365.  
  366.     ((struct PropInfo *)ColorTemplateGadgets[COLOR_RED]
  367.             .SpecialInfo)->HorizPot
  368.             = (red << 12) | (red << 8) | (red << 4) | red;
  369.     ((struct PropInfo *)ColorTemplateGadgets[COLOR_GREEN]
  370.             .SpecialInfo)->HorizPot
  371.             = (green << 12) | (green << 8) | (green << 4) | green;
  372.     ((struct PropInfo *)ColorTemplateGadgets[COLOR_BLUE]
  373.             .SpecialInfo)->HorizPot
  374.             = (blue << 12) | (blue << 8) | (blue << 4) | blue;
  375. }
  376.  
  377.  
  378. ColorRectFill(RPort, pen)
  379. struct RastPort *RPort;
  380. SHORT pen;
  381. {
  382.     SetAPen(RPort, pen);
  383.     SetDrMd(RPort, JAM2);
  384.     WaitBOVP(vp);
  385.     RectFill(RPort, COLOR_BOX_LEFT, COLOR_BOX_TOP, 
  386.         COLOR_BOX_RIGHT, COLOR_BOX_BOTTOM);
  387. }
  388.  
  389.  
  390. VOID DoColorWindow()
  391. {
  392.     struct IntuiMessage *message;
  393.     ULONG class;
  394.     struct Gadget *gadget;
  395.  
  396.     if (NOT OpenColorWindow()) return;
  397.  
  398.     FOREVER
  399.         {
  400.         Wait((1 << ColorWindow->UserPort->mp_SigBit));
  401.         while (message = (struct IntuiMessage *)GetMsg(ColorWindow->UserPort))
  402.             {
  403.             class = message->Class;
  404.             gadget = (struct Gadget *)(message->IAddress);
  405.             ReplyMsg(message);
  406.  
  407.             switch (class)
  408.                 {
  409.                 case GADGETDOWN:
  410.                 case GADGETUP:
  411.                     if (ColorGadgetGotten(gadget) == FALSE) return;
  412.                     break;
  413.                 case MOUSEMOVE:
  414.                     ModifyColors();
  415.                     break;
  416.                 }
  417.             }
  418.         }
  419. }
  420.  
  421.  
  422. InitSuperColors()
  423. {
  424.     SHORT i;
  425.  
  426.     /* OK, initialize those Super Color Gadgets! */
  427.     for (i = 0; i < 32; i++)
  428.         {
  429.         SuperColorImages[i].LeftEdge = 0;
  430.         SuperColorImages[i].TopEdge = 0;
  431.         SuperColorImages[i].Width = 15;
  432.         SuperColorImages[i].Height = 10;
  433.         SuperColorImages[i].Depth = 0;
  434.         SuperColorImages[i].ImageData = NULL;
  435.         SuperColorImages[i].PlanePick = 0;
  436.         SuperColorImages[i].PlaneOnOff = i;
  437.         SuperColorImages[i].NextImage = NULL;
  438.         }
  439. }
  440.  
  441.  
  442.